package com.zhang.servicedriveruser.controller;

import com.zhang.internalcommon.constant.DriverCarConstant;
import com.zhang.internalcommon.dto.DriverUser;
import com.zhang.internalcommon.dto.ResponseResult;
import com.zhang.internalcommon.dto.TokenResult;
import com.zhang.internalcommon.response.DriverUserExistsResponse;
import com.zhang.internalcommon.response.OrderDriverResponse;
import com.zhang.internalcommon.utils.JwtUtils;
import com.zhang.servicedriveruser.service.DriverUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 类功能描述
 *
 * @author Kye
 * @date 2025-04-12
 */
@RestController
public class UserController {

	@Autowired
	private DriverUserService driverUserService;

	@PostMapping("/user")
	public ResponseResult addUser(@RequestBody DriverUser driverUser) {
		driverUserService.addDriverUser(driverUser);
		return ResponseResult.success();
	}

	@PutMapping("/user")
	public ResponseResult updateUser(@RequestBody DriverUser driverUser) {
		ResponseResult responseResult = driverUserService.updateDriverUser(driverUser);
		return responseResult;
	}

	/**
	 * 查询司机
	 *
	 * @param driverPhone
	 * @return
	 */
	@GetMapping("/check-driver/{driverPhone}")
	public ResponseResult<DriverUserExistsResponse> getUser(@PathVariable("driverPhone") String driverPhone) {
		ResponseResult<DriverUser> user = driverUserService.getUser(driverPhone);
		DriverUser driverUserDb = user.getData();
		DriverUserExistsResponse response = new DriverUserExistsResponse();

		int ifExists = DriverCarConstant.DRIVER_EXISTS;
		if (driverUserDb == null) {
			ifExists = DriverCarConstant.DRIVER_NOT_EXISTS;
			response.setDriverPhone(driverPhone);
			response.setIfExists(ifExists);
		} else {
			response.setDriverPhone(driverUserDb.getDriverPhone());
			response.setIfExists(ifExists);
		}
		return ResponseResult.success(response);
	}

	/**
	 * 根据车辆Id查询订单需要的司机信息
	 * @param carId
	 * @return
	 */
	@GetMapping("/get-available-driver/{carId}")
	public ResponseResult<OrderDriverResponse> getAvailableDriver(@PathVariable("carId") Long carId){
		return driverUserService.getAvailableDriver(carId);
	}

	@GetMapping("/driver-car-binding-relationship")
	public ResponseResult getDriverCarBindingRelationship(@RequestParam String phone){
		return driverUserService.getDriverCarBindingRelationship(phone);
	}


}
